package cn.nucpyh.comptest;

import java.util.Scanner;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/7/29 16:58
 * @注释:异常和错误
 */
public class ErrorandException {
    //异常:编译异常(受查异常)
    //运行异常(不受查异常)
    public static void main(String[] args) {
        int[] arr = null;
        test(arr);//Process finished with exit code 0
//        System.out.println(arr[1]);JVM自动报错//Process finished with exit code 1 --> NullPointerException
    }

    private static void test(int[] a){
        Scanner s = new Scanner(System.in);
        int num = s.nextInt();
        //手动异常
        try{
            System.out.println(a[1]);
            System.out.println(10/0);
        } catch (NullPointerException n){
//            n.printStackTrace();//打印红字
            System.out.println("NullPointerException");
        }catch(ArithmeticException c){
//            c.printStackTrace();//打印红字
            System.out.println("ArithmeticException");
        }catch(Exception e){//是所有异常的父类,可以检测出所有的异常
            System.out.println("异常!");
        }finally {
            s.close();//需要手动关闭
            //jdbc也需要手动关闭
            System.out.println("finally");
        }
    }
}
